package com.zhengdongzulin.zuke.app.main.findhouse

import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.viewpager.widget.ViewPager
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.xuexiang.xui.widget.flowlayout.DefaultFlowTagAdapter
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.RoomDetails
import com.zhengdongzulin.zuke.app.main.ZKWebviewActivity
import com.zhengdongzulin.zuke.app.main.apartment.ZKReserveActivity
import com.zhengdongzulin.zuke.app.main.apartment.ZKReserveYuDingActivity
import com.zhengdongzulin.zuke.app.main.apartment.sign.ZKSign1Activity
import com.zhengdongzulin.zuke.app.main.mine.ZKIdAuthActivity
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityRoomDetailsBinding
import com.zhengdongzulin.zuke.ext.*

/**
 * @类名称: CLASS
 * @类描述: 房间详情
 * @创建人：Jeff
 * @创建时间：2021/6/9 20:59
 * @备注：
 */
class ZKRoomDetailsActivity : BaseActivity<RoomDetailsVm, ZkActivityRoomDetailsBinding>() {

    lateinit var mData: RoomDetails

    override fun getLayoutResId(): Int = R.layout.zk_activity_room_details

    override fun initVM(): RoomDetailsVm = getVM(RoomDetailsVm::class.java)

    override fun initView() {
    }

    override fun initData() {
        mViewModel.house_detail(intent.getStringExtra("id"))
    }

    override fun initListener() {
        mView.collection.setOnClickListener {
            if (checkLogin()) {
                if (mData.collectionStatus == 0) { //0 未收藏 1 收藏
                    mViewModel.collection_add(mData.houseId)
                } else {
                    mViewModel.collection_cancel(mData.houseId)
                }
            }
        }
        mView.ivVr.setOnClickListener {
            mData.vrUrl?.let {
                Intent().put("url", it).start(this, ZKWebviewActivity::class.java)
            }
        }

    }

    override fun startObserve() {
        mViewModel.house_detail.observe(this, Observer {
            it.let {
                mData = it

                when (mData.hiItemNum) {
                    "8号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_8)
                    }
                    "9号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_9)
                    }
                    "10号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_10)
                    }
                    "11号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_11)
                    }
                    "12号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_12)
                    }

                }

                mView.ivAddressView.setOnClickListener {
                    gotoHouseToNavActivity(
                        mData.lat.toString(),
                        mData.lng.toString(),
                        mData.houseName,
                        mData.hiItemNum
                    )
                }

                var imagesData = mutableListOf<BannerItem>()
                if (!it.allPicList.isNullOrEmpty()) {
                    for (roomPic in it.allPicList) {
                        imagesData.add(BannerItem().setImgUrl(roomPic.url))
                    }
                    mView.banner.setSource(imagesData)?.startScroll()
                    mView.tvCount.text = "1/${imagesData.size}"
                    mView.banner.viewPager.addOnPageChangeListener(object :
                        ViewPager.OnPageChangeListener {
                        override fun onPageScrollStateChanged(state: Int) {

                        }

                        override fun onPageScrolled(
                            position: Int,
                            positionOffset: Float,
                            positionOffsetPixels: Int
                        ) {

                        }

                        override fun onPageSelected(position: Int) {
                            mView.tvCount.text = "${position + 1}/${imagesData.size}"
                        }
                    })
                }
                if (it.collectionStatus == 0) {// 0 未收藏 1 收藏
                    mView.collection.setImageResource(R.mipmap.icon_details_collection)
                } else {
                    mView.collection.setImageResource(R.mipmap.icon_details_collection_sel)
                }
                mView.titleName.text = it.houseName
                if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                    mView.collection.gone()
                    mView.money.text = ""
                    mView.money.appendStyleSpan("面议")
                    mView.ivReserve2.setImageResource(R.mipmap.room_details_icon_en)
                    mView.ivReserve.setImageResource(R.mipmap.room_details_icon_en2)
                    mView.tvReserve.setColor(R.color.gray_ca)
                    mView.tvReserve2.setColor(R.color.gray_ca)
                    mView.sign.setBackgroundResource(R.drawable.apartment_details_bottom_button_bg_en)
                    mView.call.setBackgroundResource(R.drawable.apartment_details_bottom_button_bg_en)
                } else {
                    mView.collection.visible()
                    mView.money.text = "￥${it.rentPrice}"
                    mView.money.appendSizeSpan("/月", 0.46f)
                        .colorSpan(
                            "",
                            mView.money.text.length - 2..mView.money.text.length,
                            color(R.color.black_66)
                        )
                        .styleSpan("", 0..mView.money.text.length - 2)
                    mView.llEmpty2.setOnClickListener {
                        if (checkLogin()) {
                            mShareViewModel.roomDetails.value = mData!!
                            Intent().start(
                                this@ZKRoomDetailsActivity,
                                ZKReserveActivity::class.java
                            )
                        }
                    }
                    mView.llEmpty.setOnClickListener {
                        if (checkLogin()) {
                            mShareViewModel.roomDetails.value = mData!!
                            Intent().start(
                                this@ZKRoomDetailsActivity,
                                ZKReserveYuDingActivity::class.java
                            )
                        }
                    }
                    mView.sign.setOnClickListener {
                        if (checkLogin()) {
                            mViewModel.check_sign_online(mData.houseId)
                        }
                    }
                    mView.call.setOnClickListener {
                        if (checkLogin()) {
                            callPhoneDialog(
                                mData.houseManagerInfo.managerName,
                                mData.houseManagerInfo.managerPhone
                            ).show()
                        }
                    }
                }
                mView.flow.setAdapter(object : DefaultFlowTagAdapter(this) {
                    override fun getLayoutId(): Int {
                        return R.layout.item_apartment_details_flow
                    }
                })
                mView.flow.setItems(it.featureList)
                mView.area.text = it.area + "㎡"
                mView.direction.text = it.orientation
                mView.houseType.text = it.doorName
                mView.floor.text = it.floor
                if (it.desc.isNullOrEmpty()) {
                    mView.introduceDesc.text = "暂无描述"
                } else {
                    mView.introduceDesc.text = it.desc
                }
                mView.introduceMore.setOnClickListener {
                    var v = View.inflate(this, R.layout.popwindow_house_introduce_more, null)
                    var dialog = getBottomDialog(v, 0.4)
                    v.findViewById<View>(R.id.iv_close).setOnClickListener { dialog.dismiss() }
                    v.findViewById<View>(R.id.tv_close).setOnClickListener { dialog.dismiss() }
                    v.findViewById<TextView>(R.id.project_desc).text =
                        if (mData.desc.isNullOrEmpty())
                            "暂无描述"
                        else
                            mData.desc

                    dialog.show()
                }
                //if (introduce_desc.lineCount > 5) {
                mView.introduceMore.visible()
//                } else {
//                    introduce_more.invisible()
//                }
                mView.address.text = it.communityName
                mView.addressDesc.text = it.address
                //initMap(it)
            }
        })
        mViewModel.house_detail.state.observe(this, getObserver(mViewModel.house_detail))

        mViewModel.collection_add.observe(this, Observer {
            mData.collectionStatus = 1
            mView.collection.setImageResource(R.mipmap.icon_details_collection_sel)
            toast("收藏成功")
        })
        mViewModel.collection_add.state.observe(this, getObserver(mViewModel.collection_add))

        mViewModel.collection_cancel.observe(this, Observer {
            mData.collectionStatus = 0
            mView.collection.setImageResource(R.mipmap.icon_details_collection)
            toast("取消收藏")
        })
        mViewModel.collection_cancel.state.observe(this, getObserver(mViewModel.collection_cancel))
        mViewModel.check_sign_online.observe(this, Observer {
            if (!it.isNullOrEmpty()) {
                when (it) {//0成功，跳转到在线签约页面，1未实名认证，跳转到实名认证 2无电子模版
                    "0" -> {
                        //屏蔽在线签约
//                        Intent().put("houseId", mData.houseId)
//                            .start(this, ZKSign1Activity::class.java)


                        MaterialDialog.Builder(this)
                            .content("当前房源不支持在线签约！")
                            .positiveText("确定")
                            .positiveColor(color(R.color.color_1273f7))
                            .contentColor(color(R.color.black_66))
                            .cancelable(false)
                            .onPositive { dialog, which ->
                                dialog.dismiss()
                            }
                            .show()





                    }
                    "1" -> {
                        MaterialDialog.Builder(this)
                            .content("未实名认证，请您先进行实名认证")
                            .negativeText("取消")
                            .positiveText("去实名")
                            .positiveColor(color(R.color.color_1273f7))
                            .contentColor(color(R.color.black_66))
                            .negativeColor(color(R.color.gray_99))
                            .cancelable(false)
                            .onNegative { dialog, which ->
                                dialog.dismiss()
                            }
                            .onPositive { dialog, which ->
                                Intent().start(this, ZKIdAuthActivity::class.java)
                                dialog.dismiss()
                            }
                            .show()
                    }
                    "2" -> {
                        MaterialDialog.Builder(this)
                            .content("当前房源未配置电子合同模板\n暂不支持签约！")
                            .positiveText("确定")
                            .positiveColor(color(R.color.color_1273f7))
                            .contentColor(color(R.color.black_66))
                            .cancelable(false)
                            .onPositive { dialog, which ->
                                dialog.dismiss()
                            }
                            .show()
                    }
                    else -> {
                        toast("获取签约状态错误${mViewModel.check_sign_online.errMsg}")
                    }
                }
            } else {
                toast("获取签约状态错误${mViewModel.check_sign_online.errMsg}")
            }
        })
        mViewModel.check_sign_online.state.observe(this, getObserver(mViewModel.check_sign_online))
    }

}